package hsp.admin.app.literature.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.literature.po.PoemPO;
import hsp.admin.app.literature.service.IPoemService;
import hsp.admin.app.literature.vo.PoemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "PoemController", description = "诗词管理")
@RestController
@RequestMapping("literature/poem")
public class PoemController {

    private final IPoemService service;

    @Autowired
    public PoemController(
        IPoemService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取诗词")
    @GetMapping("{id}")
    public ApiResult<PoemPO> getPoem(@PathVariable("id") long id){
        PoemPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取诗词分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<PoemVO>> getPoemPage(@PathVariable int page, @PathVariable int size, PoemPO po){
        Page<PoemVO> records = service.pageVO(new Page<>(page, size), po);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑诗词")
    @PostMapping()
    public ApiResult<?> postOrPutPoem(@RequestBody PoemPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除诗词")
    @DeleteMapping("{id}")
    public ApiResult<?> deletePoem(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
